package com.itheima.behaviour.linstener;

import behaviour.dto.FollowBehaviorDto;
import com.alibaba.fastjson.JSON;
import com.itheima.behaviour.service.ApFollowBehaviorService;
import com.itheima.common.constants.BusinessConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @version 1.0
 * @description 说明
 * @package com.itheima.behaviour.linstener
 */
@Component
@Slf4j
public class FollowBehaviorListener {

    @Autowired
    private ApFollowBehaviorService apFollowBehaviorService;

    @KafkaListener(topics = BusinessConstants.MqConstants.FOLLOW_BEHAVIOR_TOPIC)
    public void saveFollowBehavior(ConsumerRecord<String,String> msgRecord){
        log.info("用户关注行为 start: t={},p={},msg={}",msgRecord.topic(),msgRecord.partition(),msgRecord.value());
        //解析消息内容，转为dto对象
        FollowBehaviorDto followBehaviorDto = JSON.parseObject(msgRecord.value(), FollowBehaviorDto.class);
        //保存用户行为
        apFollowBehaviorService.saveFollowBehavior(followBehaviorDto);
        log.info("用户关注行为 end: t={},p={},msg={}",msgRecord.topic(),msgRecord.partition(),msgRecord.value());
    }
}
